package com.iwanghang.movelineview.whUtil;

import android.app.Activity;
import android.graphics.Point;
import android.os.Handler;
import android.widget.RelativeLayout;

import com.iwanghang.movelineview.R;
import com.iwanghang.movelineview.whCustomView.MoveFoldLineUtil;
import com.iwanghang.movelineview.whCustomView.MoveFoldLineView;

public class AnimatorUtil {

    private static RelativeLayout relative_line_father;

    /**
     * 获取连接线父控件
     */
    private static void getLineFatherView(Activity activity) {
        relative_line_father = activity.findViewById(R.id.relative_line_father);
    }

    /**
     * 获取 包含多个球形父视图 的 中心坐标
     */
    public static void doFoldLineAnimatorPlay(final Activity activity, final Point[] foldPoints, final int speed) {
        // 获取连接线父控件
        getLineFatherView(activity);
        // 等待连接线父控件加载完成
        relative_line_father.post(new Runnable() {
            @Override
            public void run() {
                // 获取连接线父控件
                getLineFatherView(activity);
                // 播放动画 - 可以马上播放，也可以像这样，等待0.5秒再播放
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        playFoldLines(activity, foldPoints, speed);
                    }
                }, 500);
            }
        });
    }

    private static void playFoldLines(Activity activity, final Point[] foldPoints, final int speed) {
        Point[] points = MoveFoldLineUtil.getPoints(foldPoints, speed);
        MoveFoldLineView moveFoldLineView = new MoveFoldLineView(activity, points);
        moveFoldLineView.postInvalidate();
        relative_line_father.addView(moveFoldLineView);
        moveFoldLineView.StartAnimationRunnable();
    }

}
